package com.hd.hdxgddcommon.utils.sequence;


import com.hd.hdxgddcommon.utils.SpringUtils;
import com.hd.hdxgddcommon.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.util.Random;

/**
 * Created by Administrator on 2019/5/14.
 */
@Slf4j
@Deprecated
public class SequenceUtils {

    private static Sequence sequence;

    public static Sequence instance() {
        if (sequence == null)
            synchronized (Sequence.class) {
                if (sequence == null) {
                    Environment environment = SpringUtils.getBean(Environment.class);

                    String workerIdString = environment.getProperty("sequence.workerId");
                    long workerId = 0l;
                    Random random = new Random();
                    if (StringUtils.isBlank(workerIdString)) {
                        workerId = random.nextInt(32);
                    } else {
                        workerId = Long.parseLong(workerIdString);
                    }


                    String dataCenterIdString = environment.getProperty("sequence.dataCenterId");
                    long dataCenterId = 0l;
                    if (StringUtils.isBlank(dataCenterIdString)) {
                        dataCenterId = random.nextInt(32);
                    } else {
                        dataCenterId = Long.parseLong(dataCenterIdString);
                    }
                    log.info("sequence.workerId", workerId);
                    log.info("sequence.dataCenterId", dataCenterId);
                    sequence = new Sequence(workerId, dataCenterId);
                }
            }
        return sequence;
    }
}
